package com.sdry.web.controller.workplace;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hacker.base.BaseController;
import com.sdry.model.llm.Team;
import com.sdry.model.llm.User;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.model.workplace.MousseBundleSampleExample;
import com.sdry.model.workplace.MousseBundleSampleExample.Criteria;
import com.sdry.service.workplace.MousseBundleSampleServise;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;


/**
 * @ClassName: mousseBundleSampleController
 * @Description: 膜丝捆留样
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/mousseBundleSample")
public class MousseBundleSampleController extends BaseController{

    @Resource
    private MousseBundleSampleServise mousseBundleSampleServise;

    /**
     * 添加膜丝捆留样
     * @param response 响应
     * @param mousseBundleSample 膜丝捆留样实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertmousseBundleSample")
    public void insertmousseBundleSample(HttpServletRequest request, HttpServletResponse response, MousseBundleSample mousseBundleSample) throws Exception {

        int affact = 0;

        MousseBundleSampleExample example = new MousseBundleSampleExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(mousseBundleSample.getMbscode() != null && mousseBundleSample.getMbscode() != "") {
            createCriteria.andMbscodeEqualTo(mousseBundleSample.getMbscode());
        }
        createCriteria.andMbsremark1EqualTo("0");
        List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
        if(mousseBundleSamples.size() == 0){
            
        	//判断session
      		HttpSession session = request.getSession();
      		// 从session中取出用户身份信息
      		User use = (User) session.getAttribute("currentUser");
      		//当前时间
            SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
            String   date   =   sDateFormat.format(new Date());
            mousseBundleSample.setMbsoperator(use.getUserName());
            //操作人id
            mousseBundleSample.setMbsremark2(String.valueOf(use.getUid()));
            mousseBundleSample.setMbsremark5(use.getRemark3());
            //班组id
            Team team = (Team) session.getAttribute("team");
            mousseBundleSample.setMbsremark3(String.valueOf(team.getTid()));
            //班次
            mousseBundleSample.setMbsremark4(team.getCrew());
            mousseBundleSample.setMbstime(date);
        	
        	mousseBundleSample.setMbsremark1("0");
        	
        	//添加
            affact = mousseBundleSampleServise.insertSelective(mousseBundleSample);
        }
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询膜丝捆留样信息
     * @param request 请求
     * @param response 响应
     * @param rows 每页记录数
     * @param page 页数
     * @param mousseBundleSample 膜丝捆留样信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseBundleSampleList")
    public void selectmousseBundleSampleList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "page",required = false)int page,
                                   @RequestParam(value = "rows",required = false)int rows,
                                   MousseBundleSample mousseBundleSample) throws Exception {

    	PageHelper.startPage(page, rows);

        MousseBundleSampleExample example = new MousseBundleSampleExample();
        example.setOrderByClause("mbstime desc");
        Criteria createCriteria =  example.createCriteria();
    	//名称模糊
        if(mousseBundleSample.getMbscode() != null && mousseBundleSample.getMbscode() != "") {
    		createCriteria.andMbscodeLike("%"+mousseBundleSample.getMbscode()+"%");
    	}
        createCriteria.andMbsremark1EqualTo("0");
		List<MousseBundleSample> list = mousseBundleSampleServise.selectByExample(example);
		
		PageInfo<MousseBundleSample> info = new PageInfo<MousseBundleSample>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据条件查询膜丝捆留样信息
     * @param response 响应
     * @param mousseBundleSample 条件
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseBundleSampleByExample")
    public void selectmousseBundleSampleByExample(HttpServletResponse response, MousseBundleSample mousseBundleSample) throws Exception {
        MousseBundleSampleExample example = new MousseBundleSampleExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(mousseBundleSample.getMbscode() != null && mousseBundleSample.getMbscode() != "") {
            createCriteria.andMbscodeEqualTo(mousseBundleSample.getMbscode());
        }
        createCriteria.andMbsremark1EqualTo("0");
        List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
        //将对象转化为json数组
        JSONArray json = JSONArray.fromObject(mousseBundleSamples);
        ResponseUtil.write(response, json);
    }
    /**
     * 根据当前日期产品查询膜丝捆留样信息
     * @param response 响应
     * @param product 产品
     * @throws Exception 异常
     */
    @RequestMapping("/selectNumByDateAndProduct")
    public void selectNumByDateAndProduct(HttpServletRequest request,String product,String cxNum,HttpServletResponse response) throws Exception {
    	MousseBundleSampleExample example = new MousseBundleSampleExample();
    	Criteria createCriteria =  example.createCriteria();
    	//判断session
  		HttpSession session = request.getSession();
  		// 从session中取出用户身份信息
  		User use = (User) session.getAttribute("currentUser");
		String date = use.getRemark3();
    	//createCriteria.andMbstimeLike("%"+date+"%");
    	createCriteria.andMbsremark5Like("%"+date+"%");
    	List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
    	int num  = 0;
    	String size = product.substring(product.length() -2,product.length());
    	for (int i = 0; i < mousseBundleSamples.size(); i++) {
    		if(size.equals(mousseBundleSamples.get(i).getMbscode().substring(2,4)) && cxNum.equals(mousseBundleSamples.get(i).getMbscode().substring(4,6))){
    			num++;
    		}
		}
    	ResponseUtil.write(response, num);
    }
    /**
     * 根据当前年度产品查询膜丝捆留样信息
     * @param response 响应
     * @param product 产品
     * @throws Exception 异常
     */
    @RequestMapping("/selectNumByYearAndProduct")
    public void selectNumByYearAndProduct(HttpServletRequest request,String product,String cxNum,HttpServletResponse response) throws Exception {
    	MousseBundleSampleExample example = new MousseBundleSampleExample();
    	Criteria createCriteria =  example.createCriteria();
    	//判断session
  		HttpSession session = request.getSession();
  		// 从session中取出用户身份信息
  		User use = (User) session.getAttribute("currentUser");
  		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  		SimpleDateFormat format = new SimpleDateFormat("yyyy");
  		String date = format.format(dateFormat.parse(use.getRemark3()));
    	//createCriteria.andMbstimeLike("%"+date+"%");
    	createCriteria.andMbsremark5Like("%"+date+"%");
    	List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
    	int num  = 0;
    	String size = product.substring(product.length() -2,product.length());
    	for (int i = 0; i < mousseBundleSamples.size(); i++) {
    		if(size.equals(mousseBundleSamples.get(i).getMbscode().substring(2,4)) && cxNum.equals(mousseBundleSamples.get(i).getMbscode().substring(4,6))){
    			num++;
    		}
    	}
    	ResponseUtil.write(response, num);
    }
    /**
     * 根据当前日期查询膜丝捆留样信息
     * @param response 响应
     * @param product 产品
     * @throws Exception 异常
     */
    @RequestMapping("/selectNumByDate")
    public void selectNumByDate(HttpServletRequest request,String product,HttpServletResponse response) throws Exception {
    	MousseBundleSampleExample example = new MousseBundleSampleExample();
    	Criteria createCriteria =  example.createCriteria();
    	//判断session
  		HttpSession session = request.getSession();
  		// 从session中取出用户身份信息
  		User use = (User) session.getAttribute("currentUser");
  		String date = use.getRemark3();
    	//createCriteria.andMbstimeLike("%"+date+"%");
    	createCriteria.andMbsremark5Like("%"+date+"%");
    	List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
    	int num  = mousseBundleSamples.size();
    	ResponseUtil.write(response, num);
    }
    /**
     * 根据id查询膜丝捆留样信息
     * @param response 响应
     * @param id 膜丝捆留样id
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseBundleSampleById")
    public void selectmousseBundleSampleById(HttpServletResponse response, Long id) throws Exception {

        MousseBundleSample mousseBundleSample = mousseBundleSampleServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(mousseBundleSample);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改膜丝捆留样信息
     * @param response 响应
     * @param mousseBundleSample 膜丝捆留样信息
     * @throws Exception 异常
     */
    @RequestMapping("/editmousseBundleSample")
    public void editmousseBundleSample( HttpServletResponse response,
                              MousseBundleSample mousseBundleSample) throws Exception {

        int affct = 0;

        MousseBundleSampleExample example = new MousseBundleSampleExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(mousseBundleSample.getMbscode() != null && mousseBundleSample.getMbscode() != "") {
            createCriteria.andMbscodeEqualTo(mousseBundleSample.getMbscode());
        }
        createCriteria.andMbsremark1EqualTo("0");
        List<MousseBundleSample> mousseBundleSamples = mousseBundleSampleServise.selectByExample(example);
        if(mousseBundleSamples.size() == 0){
            //修改
            affct = mousseBundleSampleServise.updateByPrimaryKeySelective(mousseBundleSample);
        }else if(mousseBundleSamples.size() == 1 && mousseBundleSamples.get(0).getMbsid().equals(mousseBundleSample.getMbsid())){
            affct = mousseBundleSampleServise.updateByPrimaryKeySelective(mousseBundleSample);
        }
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除
     * @param response 响应
     * @param ids 膜丝捆留样id
     * @throws Exception 异常
     */
    @RequestMapping("/deletemousseBundleSample")
    public void deletemousseBundleSample( HttpServletResponse response, String ids) throws Exception {
    	List<Long> splitListL = StringUtil.splitListL(ids, ",");
   	 	int affact = 0;
   	 	for (int i = 0; i < splitListL.size(); i++) {
 			 MousseBundleSample mousseBundleSample = new MousseBundleSample();
 			 mousseBundleSample.setMbsid(splitListL.get(i));
 			 mousseBundleSample.setMbsremark1("1");
 			affact += mousseBundleSampleServise.updateByPrimaryKeySelective(mousseBundleSample);
		}
        //int affact = mousseBundleSampleServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }


}
